<template>
	<view class="detail">
		<view class="detail_nv">
			{{currentTitle}}
		</view>
		<view class="" style="position: fixed;top: 0;left: 0;width: 750rpx;z-index: 99999;" v-if="ceShow">
			<u-notice-bar mode="horizontal" :list="noticeList"></u-notice-bar>
		</view>
		<u-modal v-model="show" :show-title="false" width="690" :show-confirm-button="false" z-index="20">
			<view class="slot-content">
				<view class="dialogMain">
					<view class="dialogMain_top">
						<image src="../../static/img/close.png" mode="" @click="closeDialog"></image>
					</view>
					<view class="" style="padding: 0 18rpx;border-bottom: 1rpx solid #e2e2e2;">
						<!-- <span>{{nowText}}</span> -->
						<u-input v-model="value" type="textarea" :border="false" :height="300" />
					</view>
					<view class="dialogMain_bottom">
						<view class="" @click="submit">
							回复
						</view>
					</view>
				</view>
			</view>
		</u-modal>
		<scroll-view class="scroll-view" :scroll-y="true" :refresher-enabled="enabled" 
			@refresherrefresh="doRefresh" :refresher-triggered="isRefreshing">
			<view class="detailRow" v-for="(item,index) in list" :key="index">
				<view class="timeRow">
					<view class="timebox">
						{{timestamp(item.createtime)}}
					</view>
				</view>
				<view class="">
					<view class="detailRow_right" v-if="item.sender_id == userInfo.id">
						<!-- <text>{{timestamp(item.createtime)}}</text> -->
						<span>{{item.sender.nickname}}</span>
						<image :src="item.sender.avatar" mode=""></image>
					</view>
					<view class="detailRow_left" v-else>
						<image :src="item.sender.avatar" mode=""></image>
						<span>{{item.sender.nickname}}</span>
						<text>{{timestamp(item.createtime)}}</text>
					</view>
					<view class="detailRow_content" :style="item.sender_id == userInfo.id?'justify-content:flex-end;':''">
						<view class="detailRow_content_main" :style="item.sender_id == userInfo.id?'':'background-color:#fff;'">
							{{item.content}}
						</view>
					</view>
				</view>
			</view>
		</scroll-view>

		<view class="detail_footer">
			<view class="vsInput" @click="show = true">
				点击回复
			</view>
		</view>
	</view>
</template>

<script>
	import {
		mapGetters
	} from 'vuex';
	import {
		baseUrl
	} from '@/config/config.js'
	import {
		getMyLayListDetail,
		getMyLayDetailReply,
		getMyLayListDetailTwo
	} from '@/api/index.js'
	export default {
		data() {
			return {
				enabled:true,
				isRefreshing:false,
				status:'loadmore',
				id: '',
				page: 1,
				limit: 10,
				list: [],
				show: false,
				value: '',
				userInfo: {},
				ceShow: false,
				noticeList: [],
				currentTitle:''
			}
		},
		watch: {
			socketMessage(newVal, oldVal) {
				if (newVal) {
					this.ceShow = true
					this.noticeList = [newVal.slice(0,-1)]
					setTimeout(() => {
						this.ceShow = false
					}, 10000)
				}
			}
		},
		computed: {
			...mapGetters(['getSocketMessage']),
			socketMessage() {
				return this.getSocketMessage;
			}
		},
		methods: {
			doRefresh(){
				// console.log(3123);
				if(this.status == 'nomore'){
					this.enabled = false
					return uni.showToast({
						icon:'none',
						title:'没有更多资讯记录了'
					})
				}
				this.isRefreshing = true
				this.page++
				this.getDetail()
			},
			timestamp(timestamp) {
				var date = new Date(timestamp * 1000); // 转换为毫秒
				var year = date.getFullYear();
				var month = ('0' + (date.getMonth() + 1)).slice(-2);
				var day = ('0' + date.getDate()).slice(-2);
				var hours = ('0' + date.getHours()).slice(-2);
				var minutes = ('0' + date.getMinutes()).slice(-2);
				var seconds = ('0' + date.getSeconds()).slice(-2);

				return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds;
			},
			async submit() {
				if (this.value == '') {
					return uni.showToast({
						icon: 'none',
						title: '请输入内容'
					})
				}
				let data = {
					id: this.id,
					content: this.value,
				}
				const res = await getMyLayDetailReply(data)
				// console.log(res);
				uni.showToast({
					icon: 'none',
					title: res.msg
				})
				this.closeDialog()
				this.page = 1
				this.list = []
				this.getDetail()
			},
			closeDialog() {
				this.show = false
				this.value = ''
			},
			async getDetail() {
				let data = {
					id: this.id,
					page: this.page,
					limit: this.limit
				}
				const res = await getMyLayListDetailTwo(data)
				this.list = [...res.data.list.data.reverse(), ...this.list]
				this.currentTitle = res.data.title
				if(res.data.list.data.length<this.limit){
					this.status = 'nomore'
				}else{
					this.status = 'loadmore'
				}
				this.isRefreshing = false
			}
		},
		onLoad(e) {
			this.ws.pageFun(this.pageDataLoad, this);
			this.baseUrl = baseUrl
			this.id = e.id
			this.userInfo = uni.getStorageSync('userInfo')
			// console.log(e.id);
			this.getDetail()
		}
	}
</script>

<style scoped lang="scss">
	.detail {
		height: 100vh;
		background-color: #f5f5f5;
		position: relative;
		.detail_nv{
			padding: 20rpx;
			font-size: 24rpx;
			color: #333;
			background-color: #daefef;
		}
		
		.scroll-view{
			height: 100%;
			padding-bottom: 120rpx;
			padding-top: 40rpx;
		}

		.dialogMain {
			padding: 30rpx;

			.dialogMain_top {
				display: flex;
				justify-content: flex-end;

				image {
					width: 23rpx;
					height: 23rpx;
				}
			}

			.dialogMain_bottom {
				display: flex;
				justify-content: flex-end;
				color: #045B9B;
				padding-right: 18rpx;
				padding-bottom: 16rpx;
				margin-top: 16rpx;
			}
		}

		.detail_footer {
			position: fixed;
			width: 750rpx;
			height: 100rpx;
			left: 0;
			bottom: 0;
			display: flex;
			align-items: center;
			justify-content: center;
			background-color: #fff;
			
			.vsInput {
				width: 690rpx;
				background-color: #c4c4c4;
				height: 60rpx;
				border-radius: 50rpx;
				padding: 0 40rpx;
				line-height: 60rpx;
				font-size: 24rpx;
				color: #999;
			}
		}

		.detailRow {
			margin-bottom: 40rpx;
			padding: 0 30rpx;
			.timeRow{
				display: flex;
				justify-content: center;
				margin: 16rpx 0;
				.timebox{
					color: #fff;
					background-color: #dadada;
					padding: 4rpx 10rpx;
					font-size: 22rpx;
					border-radius: 4rpx;
				}
			}

			.detailRow_left,
			.detailRow_right {
				display: flex;
				align-items: center;
				width: 100%;

			}

			.detailRow_left {
				justify-content: flex-start;

				image {
					width: 60rpx;
					height: 60rpx;
					margin-right: 20rpx;
					border-radius: 50%;
				}

				span {
					font-size: 22rpx;
					margin-right: 20rpx;
				}

				text {
					font-size: 22rpx;
					color: #666;
				}
			}

			.detailRow_right {
				justify-content: flex-end;

				image {
					width: 60rpx;
					height: 60rpx;
					margin-left: 20rpx;
					border-radius: 50%;
				}

				span {
					font-size: 22rpx;
					margin-left: 20rpx;
				}

				text {
					font-size: 22rpx;
					color: #666;
				}
			}

			.detailRow_content {
				display: flex;

				.detailRow_content_main {
					margin: 10rpx 0;
					padding: 10rpx 30rpx;
					background-color: #89d961;
					width: fit-content;
					border-radius: 10rpx;
					max-width: 450rpx;
				}
			}
		}
	}
</style>